class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        int n = s.size();
        map<string, bool> mp;
        for (auto x : wordDict) {
            mp[x] = 1;
        }
        vector<bool>f(n + 1, 0);
        f[0] = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < i; j++) {
                string s1 = s.substr(j, i - j);
                if (mp[s1] && f[j]) f[i] = 1;
            }
        }
        return f[n];
    }
};